package com.tanhua.server.service;



import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.server.api.VoiceApi;
import com.tanhua.dubbo.server.pojo.Voice;
import com.tanhua.dubbo.server.pojo.VoiceVo;
import com.tanhua.server.mapper.UserInfoMapper;
import com.tanhua.server.pojo.User;

import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class VoiceService {

    @Reference(version = "1.0.0")
    private VoiceApi voiceApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    protected FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;


    /**
     * 上传语音
     * @param voiceFile
     * @return
     */
    public ResponseEntity<Void> saveVoice(MultipartFile voiceFile) {
        try {
            //确认登录
            User user = UserThreadLocal.get();
            //新建上传语音对象
            VoiceVo voiceVo = new VoiceVo();
            voiceVo.setUserId(user.getId());
            voiceVo.setCreated(System.currentTimeMillis());

            //生成voiceurl
            StorePath storePath = storageClient.uploadFile(voiceFile.getInputStream(),
                    voiceFile.getSize(),
                    StringUtils.substringAfter(voiceFile.getOriginalFilename(), "."),
                    null);
            String voiceurl=fdfsWebServer.getWebServerUrl() + "/" + storePath.getFullPath();

            voiceVo.setSoundUrl(voiceurl);

            //调用save方法完成上传
            voiceApi .save(voiceVo);
            return ResponseEntity.ok(null);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

    /**
     * 接收语音
     * @return
     */
    public Voice getVoice() {


        //获取当前用户ID
        User user = UserThreadLocal.get();

        //当前用户ID获取当前用户详细信息
        UserInfo userInfo = userInfoService.queryUserinfoById(user.getId());

        //获取剩余次数
        Integer times = userInfo.getTimes();
      //  List<VoiceVo> voiceVos  = new ArrayList<>();

        List<VoiceVo> voiceVoslist = voiceApi.getVoice();

        //新建查询集合,去除自我喜欢
        ArrayList<VoiceVo> voiceVos = new ArrayList<>();

        //遍历查询返回都集合,去除自己,添加到查询集合
        for (VoiceVo voiceVo : voiceVoslist) {
            if (!(voiceVo.getUserId()==user.getId())){
                voiceVos.add(voiceVo);
            }
        }


        //     for (int i = 0; i <voiceVos.size() ; i++) {

        //存在次数开始随机获取一个语音
            if (times>0){
                VoiceVo voiceVo = voiceVos.get((int) (voiceVos.size()*Math.random()));
              //  voiceVos.remove(voiceVo);
                UserInfo voiceUser = userInfoService.queryUserinfoById(voiceVo.getUserId());

                //新建一个完整的语音对象
                Voice voice = new Voice();

                //设置数据
                voice.setId(voiceVo.getUserId().intValue());
                voice.setAvatar(voiceUser.getLogo());
                voice.setNickname(voiceUser.getNickName());
                voice.setGender(voiceUser.getSex().toString());
                voice.setAge(voiceUser.getAge());
                voice.setSoundUrl(voiceVo.getSoundUrl());
                voice.setRemainingTimes(times);

                //计数器减一
                times=times-1;
                userInfo.setTimes(times);
                userInfoService.updateUserInfoByUserId(userInfo);

                //返回查到的语音
                return voice;
            }
    //    }


        return null;

    }
}
